/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com 

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _TRI_MESH_H_
#define _TRI_MESH_H_
 
//Include library header
#include "base/vector.h"
#include "entity.h"

#include <iostream>
#include <fstream>
#include <string>

namespace tri{

class TTexCoord{
public:
	TTexCoord(float u,float v);
	
	float u;
	float v;
};

class TPolygonFace{
public:
	TPolygonFace();
	
	void addData(unsigned int v, unsigned int vt , unsigned int vn);
	unsigned int size() const;
	unsigned int getVertex(unsigned int index);
	unsigned int getNormal(unsigned int index);
	unsigned int getTexCoord(unsigned int index);
	
private:
	std::vector<unsigned int> mVertexIndices;
	std::vector<unsigned int> mTexCoordIndices;
	std::vector<unsigned int> mNormalIndices;
};



class TMesh : public tri::TEntity { 
public:
	TMesh();
    virtual ~TMesh();

    virtual void display();
    
    static TMesh* loadObjMesh(const char* p_filename);
    

    static GLuint renderMesh(std::vector<tri::TVector>& VertexBuffer,
    					  std::vector<tri::TVector>& NormalBuffer,
    							std::vector<TPolygonFace> &Faces,
    							std::vector<TTexCoord>& TexCBuffer,
							    tri::TMaterial& Material,
							    std::string Texture,
							    GLuint id);
    
    static void renderMeshCellShaded(std::vector<tri::TVector>& VertexBuffer,
			  std::vector<tri::TVector>& NormalBuffer,
					std::vector<TPolygonFace> &Faces,
					std::vector<TTexCoord>& TexCBuffer,
				    tri::TMaterial& Material,
				    std::string Texture,tri::TMatrix* mat);
    void clear();
    
    static void initCellShadingTexture();
    static void bindShader();
    
    std::vector<tri::TVector> mVertexBuffer;
    std::vector<tri::TVector> mNormalBuffer;
    std::map< std::string, std::vector<TPolygonFace> > mFaceBuffer;
    std::vector<TTexCoord> mTexCBuffer;
    std::map< std::string, tri::TMaterial > mMaterialBuffer;
    void setModelTexture(std::string image, std::string model="");
    
private:
	GLuint mDisplayID;
    static unsigned int CELL_SHADING_TEXTURE_ID;
};

/*
class TModel3D : public tri::TEntity{
private:
	std::string mModel;

	static std::map<std::string,TMesh*> mModelList;
	
public:
	TModel3D();
	virtual ~TModel3D();
	virtual void display();
	
	void setModel(std::string model);
	void setModelTexture(std::string, std::string);
	static bool loadModel(std::string key,std::string _file = "");
	static void deleteModel(std::string key);
	static bool exists(std::string key);
	
private:
	std::map<std::string, std::string> mTextures;
};

*/

}//close namespace

#endif
